<template>
  <div class="display-search-div">
    <div class="header">
      <slot name="header"></slot>
    </div>
    <el-form class="display-search-form">
      <el-row>
          <template v-for="item in searchProps" :key="item.label" >
            <el-col v-bind="item.colLayout || colLayout" v-if="!(item.vIf === false) && (!showToggle || !item.defaultHidden || isToggle)">
              <el-form-item v-if="!item.isHidden" :label="item.label" :rules="item.rules"
                :style="item.itemStyle || itemStyle" :label-width="item.labelWidth || labelWidth">
                <template v-if="item.type === 'input' || item.type === 'password'">
                  <el-input :placeholder="item.placeholder" v-bind="item.otherOptions"
                    :show-password="item.type === 'password'" v-model="subModelValue[`${item.field}`]"
                    @keyup.enter="queryData" />
                </template>
                <template v-else-if="item.type === 'select'">
                  <el-select clearable popper-class="detailselect" :placeholder="item.placeholder"
                    v-bind="item.otherOptions" style="width: 100%" v-model="subModelValue[`${item.field}`]">
                    <el-option v-for="option in codeCollection[`${item.field}`] || []" :key="option.value"
                      :value="option.value" :label="option.title"></el-option>
                  </el-select>
                </template>
                <template v-else-if="item.type === 'datepicker'">
                  <el-date-picker style="width: 100%" v-bind="item.otherOptions"
                    v-model="subModelValue[`${item.field}`]"></el-date-picker>
                </template>
              </el-form-item>
            </el-col>
          </template>
          <el-col :span="6">
            <el-form-item :style="itemStyle">
              <template #label>
                <div v-if="showToggle" class="ml10" @click="isToggle = !isToggle" style="display: flex;align-items: center;color: rgb(64,158,255);cursor: pointer;">
                  <span>{{ isToggle ? '收筛选' : '展筛选' }}</span>
                  <el-icon>
                      <ArrowUp v-if="isToggle"/>
                      <ArrowDown v-else/>
                  </el-icon>
                </div>
              </template>
              <el-button type="primary" @click="queryData">
                查询
              </el-button>
              <el-button type="warning" @click="resetQueryData">
                重置
              </el-button>
              <el-button type="success" v-if="showExportExcel" @click="exportExcel" :loading="exportExcelLoading"
                :disabled="exportExcelLoading">
                导出
              </el-button>
            </el-form-item>
          </el-col>
      </el-row>
    </el-form>
    <div class="footer">
      <slot name="footer"></slot>
    </div>
  </div>
</template>

<script>
import { watch, ref, computed, onMounted, onBeforeMount } from "vue";
import {  ArrowUp,ArrowDown } from '@element-plus/icons-vue'
export default {
  components: {
    ArrowUp,ArrowDown
  },
  props: {
    modelValue: {
      type: Object,
      required: true
    },
    searchProps: {
      type: Array,
      default: () => []
    },
    labelWidth: {
      type: String,
      default: '80px'
    },
    itemStyle: {
      type: Object,
      default: () => ({ padding: '10px 40px' })
    },
    showExportExcel: {//是否显示导出excel按钮
      type: Boolean,
      require: false,
      default: () => false
    },
    exportExcelLoading: {//导出excel按钮是否在加载中
      type: Boolean,
      require: false,
      default: () => false
    },
    colLayout: {
      type: Object,
      default: () => ({
        xl: 6,
        md: 12,
        sm: 24,
        xs: 24
      })
    },
    showSearch: {
      type: Boolean,
      required: false,
      default: () => true
    }
  },
  emits: ['update:modelValue', 'queryData', 'resetQueryData', 'exportExcel'],
  setup(props, { emit }) {
    const showToggle = ref(false)//是否展示折叠搜索条件
    const isToggle = ref(false)//折叠状态,默认是false,收起的
    const codeCollection = ref({})
    const subModelValue = ref({})
    const queryData = () => {
      emit('update:modelValue', JSON.parse(JSON.stringify(subModelValue.value)))
      emit('queryData')
    }
    const resetQueryData = () => {
      subModelValue.value = {}
      emit('resetQueryData')
    }
    const exportExcel = () => {
      emit('exportExcel')
    }
    const apendCodeAndJudgeShowToggle = async () => {
      for (const item of props.searchProps) {
        //1.判断是否需要显示'是否展示折叠搜索条件'
        if(item.defaultHidden){//如果有一个搜索项是需要隐藏的,那么'是否展示折叠搜索条件'的值为true
          showToggle.value = true
        }
        //2.初始化搜索条件需要的code
        if (item.type === 'select') {
          let options = item.options
          if (options instanceof Array) {
            codeCollection.value[`${item.field}`] = options
          } else if (typeof (options) === 'function') {
            codeCollection.value[`${item.field}`] = await options() || []
          }
        }
      }
    }
    apendCodeAndJudgeShowToggle();
    return {
      queryData,
      resetQueryData,
      subModelValue,
      codeCollection,
      exportExcel,
      showToggle,
      isToggle
    }
  }
};
</script>
<style scoped>
:deep() .el-form-item__content{
  flex-wrap: nowrap;
}
</style>
  